from Libs import MysqlModel
from urllib.parse import urlparse


class Urls():

    @staticmethod
    def select():
        MysqlApi = MysqlModel.mysql()
        data = MysqlApi.select("select * from urls ")

        print(data)

    @staticmethod
    def insert(url):
        MysqlApi = MysqlModel.mysql()

        urlInfo = urlparse(url)
        host = urlInfo.netloc

        # SQL 插入语句
        sql = "INSERT INTO urls(`host`,  `url`, `status`  " \
              "VALUES ('%s', '%s', '%s' )" % (host, url, 0)

        MysqlApi.insert(sql)

    @staticmethod
    def insertMulti(urlList):
        MysqlApi = MysqlModel.mysql()

        # SQL 插入语句
        sql = "INSERT INTO urls(`host`,  `url`, `status`)  VALUES "

        for url in urlList:
            urlInfo = urlparse(url)
            host = urlInfo.netloc
            sql += "('%s', '%s', '%s' )," % (host, url, 0)

        sql = sql.rstrip(",")
        MysqlApi.insert(sql)

    @staticmethod
    def delete():
        MysqlApi = MysqlModel.mysql()

        # SQL 插入语句
        sql = "DELETE FROM urls WHERE AGE = %s" % (20)

        MysqlApi.insert(sql)
